<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestInputChecker extends PHPUnit_Framework_TestCase {


	public function test_check_input() {

		$checker = new InputChecker();

		$this->add_alerter_to_input_checker($checker, $this->never());

		$alert_value = "test";

		$actual = $checker->check_input(true, $alert_value);	
		$this->assertTrue($actual);
	}
		
	public function test_check_input_false() {
		$checker = new InputChecker();

		$alerter = $this->getMock('Alerter');
		$checker->set_alerter($alerter);

		$alert_value = "test";

		$alerter->expects($this->once())
				->method('add_alert')
				->with($alert_value);

		$actual = $checker->check_input(false, $alert_value);
		$this->assertFalse($actual);
	}
	
	public function test_check_exists_where_no_fieldname() {
		
		$checker = new InputChecker();

		$this->add_alerter_to_input_checker($checker, $this->never());

		$actual = $checker->check_exists('validstring');

		$this->assertTrue($actual);

		$alertValue = "Geen invoer voor veld []"; //TODO: localize

		$alerter = $this->getMock('Alerter');

		$this->add_alerter_to_input_checker($checker, $this->once(), $alertValue);

		$actual = $checker->check_exists('');
		$this->assertFalse($actual);

		$this->add_alerter_to_input_checker($checker, $this->once(), $alertValue);

		$actual = $checker->check_exists(' ');
		$this->assertFalse($actual);
	}


	public function test_check_exists_where_fieldname() {
		
		$checker = new InputChecker();

		$fieldName = "field";

		$this->add_alerter_to_input_checker($checker, $this->never());

		$actual = $checker->check_exists('validstring', $field);

		$this->assertTrue($actual);

		$alert_value = "Geen invoer voor veld [$field]"; //TODO: localize

		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);

		$actual = $checker->check_exists('', $field);
		$this->assertFalse($actual);

		$alert_value = "Geen invoer voor veld [$field]"; //TODO: localize

		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);

		$actual = $checker->check_exists(' ', $field);
		$this->assertFalse($actual);
	}

	public function test_check_url_where_no_fieldname() {
		
		$checker = new InputChecker();

		$this->add_alerter_to_input_checker($checker, $this->never());

		$actual = $checker->check_url('http://www.http.com');

		$this->assertTrue($actual);

		$this->add_alerter_to_input_checker($checker, $this->never());
		$actual = $checker->check_url('https://www.https.com');

		$this->assertTrue($actual);

		$alert_value = " begint niet met 'http://'"; //TODO: localize //TODO: what with leading space in string?
		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);

		$actual = $checker->check_url('');
		$this->assertFalse($actual);

		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$actual = $checker->check_url(' ');
		$this->assertFalse($actual);
	}

	public function test_check_url_where_fieldname() {
		
		$checker = new InputChecker();

		$fieldName = "field";

		$this->add_alerter_to_input_checker($checker, $this->never());
		$actual = $checker->check_url('http://www.http.com', $fieldName);

		$this->assertTrue($actual);

		$this->add_alerter_to_input_checker($checker, $this->never());
		$actual = $checker->check_url('https://www.https.com', $fieldName);

		$this->assertTrue($actual);

		$alert_value = "$fieldName begint niet met 'http://'"; //TODO: localize //TODO: what with leading space in string?

		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$actual = $checker->check_url('', $fieldName);
		$this->assertFalse($actual);

		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$actual = $checker->check_url(' ', $fieldName);
		$this->assertFalse($actual);
	}

	public function test_check_tags() {
		
		$checker = new InputChecker();

		$fieldName = "field";
		$correctString = "this string should be correct";
		$incorrectString = "<b> this string should be incorrect</b> <br />";

		$this->add_alerter_to_input_checker($checker, $this->never());
		$actual = $checker->check_tags($correctString, $fieldName);

		$this->assertTrue($actual);

		$alert_value = "Er is voor deze invoer geen HTML toegestaan [$fieldName]";

		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$actual = $checker->check_tags($incorrectString, $fieldName);

		$this->assertFalse($actual);

	}

	public function test_get_tags() {
		
		$checker = new InputChecker();
		$actual = $checker->get_tags("<strong><em></em></strong>");

		$this->assertEquals("strong", $actual[0]);
		$this->assertEquals("em", $actual[1]);
		$this->assertEquals("/em", $actual[2]);
		$this->assertEquals("/strong", $actual[3]);

		$actual = $checker->get_tags("<strong>    some other stuff.<em 
									  class=\"aClass\"></em></strong>");

		$this->assertEquals("strong", $actual[0]);
		$this->assertEquals("em", $actual[1]);
		$this->assertEquals("/em", $actual[2]);
		$this->assertEquals("/strong", $actual[3]);
	}

	public function test_check_exists_and_tags() {

		$checker = new InputChecker();

		$this->add_alerter_to_input_checker($checker, $this->never());

		$fieldName = "field";
		$correctString = "this string should be correct";
		$incorrectString = "<b> this string should be incorrect</b> <br />";
		$emptyString = "    ";
		$values = array("correct" => $correctString,
						"incorrect" => $incorrectString,
						"empty" => $emptyString);

		$actual = $checker->checkExistsAndTags($values, "correct", $title);

		$this->assertTrue($actual);

		$alert_value = 'Er is voor deze invoer geen HTML toegestaan []';
		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$actual = $checker->checkExistsAndTags($values, "incorrect", $title);

		$this->assertFalse($actual);

		$alert_value = 'Geen invoer voor veld []';
		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$actual = $checker->checkExistsAndTags($values, "empty", $title);

		$this->assertFalse($actual);
	}

	public function test_retrieve_default_tags() {

		$checker = new InputChecker();	

		$actual = $checker->retrieve_default_tags();

		$this->assertTrue(in_array("table", $actual));
		$this->assertTrue(in_array("font", $actual));
		$this->assertTrue(in_array("blockquote", $actual));
		$this->assertTrue(in_array("center", $actual));
		$this->assertTrue(in_array("h1", $actual));
		$this->assertTrue(in_array("h2", $actual));
		$this->assertTrue(in_array("h3", $actual));
		$this->assertTrue(in_array("h4", $actual));
		$this->assertTrue(in_array("tr", $actual));
		$this->assertTrue(in_array("td", $actual));
		$this->assertTrue(in_array("ul", $actual));
		$this->assertTrue(in_array("li", $actual));
		$this->assertTrue(in_array("ol", $actual));
		$this->assertTrue(in_array("sup", $actual));
		$this->assertTrue(in_array("sub", $actual));
		$this->assertTrue(in_array("small", $actual));
		$this->assertTrue(in_array("quote", $actual));
		$this->assertTrue(in_array("dialect", $actual));
		$this->assertTrue(in_array("a", $actual));
		$this->assertTrue(in_array("b", $actual));
		$this->assertTrue(in_array("i", $actual));
		$this->assertTrue(in_array("u", $actual));
							  
		return $checked_tags;
	}

	public function test_check_tag_closures() {

		$checker = new InputChecker();


		$this->add_alerter_to_input_checker($checker, $this->never());
		$no_tags = "this piece of text has no tags whatsoever";

		$this->assertTrue($checker->check_tag_closures($no_tags));

		$this->add_alerter_to_input_checker($checker, $this->never());
		$correct_with_one_tag = "<i>there's one opening tag, and </i> one closing tag";

		$this->assertTrue($checker->check_tag_closures($correct_with_one_tag));

		$alert_value = "Ongelijk aantal openende en sluitende &lt;i&gt; tags";
		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$incorrect_with_one_tag = "<i>there's only an opening tag";

		$this->assertFalse($checker->check_tag_closures($incorrect_with_one_tag));
	}
	
	public function test_check_tag_closures_with_nested_tags() {

		$checker = new InputChecker();

		$this->add_alerter_to_input_checker($checker, $this->never());

		$correct_with_nested_tags = "<quote>there's three <span class=\"test\"> opening tags</span>, 
								<i>and</i> three closing tags</quote>";

		$this->assertTrue($checker->check_tag_closures($correct_with_nested_tags));

		$incorrect_with_nested_tags = "<quote>there's three <span class=\"test\"> opening tags</span>, 
								<i>and three closing tags</quote>";

		$alert_value = "Ongelijk aantal openende en sluitende &lt;i&gt; tags";
		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);

		$this->assertFalse($checker->check_tag_closures($incorrect_with_nested_tags));
	}

	public function test_check_tag_closures_with_closing_before_opening() {
		
		$checker = new InputChecker();

		$this->add_alerter_to_input_checker($checker, $this->never());

		$incorrect_closing_before_opening = "</i>closing tag first<i> followed by opening";
		$alert_value = "Afsluitende &lt;i&gt; tag voor openende";

		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$this->assertFalse($checker->check_tag_closures($incorrect_closing_before_opening));
	}

	public function test_check_tag_closures_with_custom_checked_tags() {
		$checker = new InputChecker();

		$this->add_alerter_to_input_checker($checker, $this->never());

		$incorrect_with_nested_tags = "<quote>there's three <span class=\"test\"> opening tags</span>, 
								<i>and three closing tags</quote>";

		$custom_tags = array("quote", "span");

		$this->assertTrue($checker->check_tag_closures($incorrect_with_nested_tags, $custom_tags));

		$custom_tags = array("i", "span");
		$alert_value = "Ongelijk aantal openende en sluitende &lt;i&gt; tags";

		$this->add_alerter_to_input_checker($checker, $this->once(), $alert_value);
		$this->assertFalse($checker->check_tag_closures($incorrect_with_nested_tags, $custom_tags));
	}

	public function test_check_tags_markup_with_correct_tags() {
		$checker = new InputChecker();
		
		$this->add_alerter_to_input_checker($checker, $this->never());

		$correctString = "<b><i>this string <u>is</u> <sup>completely</sup> <sub>correct</sub></i></b>";

		$this->assertTrue($checker->check_tags_markup($correctString));
	}

	public function test_check_tags_markup_with_incorrect_tags() {
		$checker = new InputChecker();


		$incorrectString = "<b><i>this string <strong>is</strong> <sup>slightly</sup> <sub>incorrect</sub></i></b>";
		$field_name = "field";

		$alert_value = "/tag '\/?strong' wordt niet herkend of toegelaten voor deze invoer \[field\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($alert_value));
		$this->assertFalse($checker->check_tags_markup($incorrectString, $field_name));
	}

	public function test_check_tags_advanced_markup_with_correct_tags() {
		$checker = new InputChecker();

		$this->add_alerter_to_input_checker($checker, $this->never());

		$correct_string1 = "<b><i>this string <u>is</u> <sup>completely</sup> <sub>correct</sub></i></b>";

		$this->assertTrue($checker->check_tags_advanced($correct_string1));

		$this->add_alerter_to_input_checker($checker, $this->never());
		$correct_string2 = "<img src=\"http://www.nowhere.com\" />this <a href=\"http://www.nowhere.com\">string</a> is completely correct";
		$this->assertTrue($checker->check_tags_advanced($correct_string2));

		$this->add_alerter_to_input_checker($checker, $this->never());
		$correct_string3 = "<table><tr><td>this</td><td>string</td></tr></table> is completely correct";
		$this->assertTrue($checker->check_tags_advanced($correct_string3));

		$correct_string4 = "<blockquote>this string is said to be <quote>completely correct</quote></blockquote>";

		$this->add_alerter_to_input_checker($checker, $this->never());
		$this->assertTrue($checker->check_tags_advanced($correct_string4));

		$this->add_alerter_to_input_checker($checker, $this->never());
		$correct_string5 = "<dialect>this string is <bullshit>completely <font color=\"red\">correct</font></bullshit></dialect>";
		$this->assertTrue($checker->check_tags_advanced($correct_string5));

		$this->add_alerter_to_input_checker($checker, $this->never());
		$correct_string6 = "<ol><li>this string</li></ol><ul><li>is completely</li><li>correct</li></ul>";
		$this->assertTrue($checker->check_tags_advanced($correct_string6));

		$this->add_alerter_to_input_checker($checker, $this->never());
		$correct_string7 = "<h1><small>this string</small></h1><h2>is</h2> <h4>completely</h4><h3>correct</h3>";
		$this->assertTrue($checker->check_tags_advanced($correct_string7));

		$this->add_alerter_to_input_checker($checker, $this->never());
		$correct_string8 = "<p>this <center>string</center> is <pre>completely</pre> <tt>correct</tt></p>";
		$this->assertTrue($checker->check_tags_advanced($correct_string8));

		$this->add_alerter_to_input_checker($checker, $this->never());
		$correct_string9 = "this<br/> string<br> is<hr/> completely<hr> correct";
		$this->assertTrue($checker->check_tags_advanced($correct_string9));
	}

	public function test_check_tags_advanced_markup_with_incorrect_tags() {
		$checker = new InputChecker();

		$field_name = "field";

		$incorrect_string1 = "<b><i>this string <em>is</em> <sup>slightly</sup> <sub>incorrect</sub></i></b>";

		$expected1 = "/tag '\/?em' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";

		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected1));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string1));

		$incorrect_string2 = "<img src=\"http://www.nowhere.com\" />this <link href=\"http://www.nowhere.com\">string</link> is slightly incorrect";

		$expected2 = "/tag '\/?link' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected2));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string2));

		$incorrect_string3 = "<table><tr><th>this</th><td>string</td></tr></table> is slightly incorrect";

		$expected3 = "/tag '\/?th' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected3));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string3));

		$incorrect_string4 = "<blockquote>this string is said to be <script type=\"text/javascript\" >slightly incorrect</script></blockquote>";

		$expected4 = "/tag '\/?script' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected4));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string4));

		$incorrect_string5 = "<dialect>this string is <bullshit>slightly <span style=\"font-color: red\">incorrect</span></bullshit></dialect>";

		$expected5 = "/tag '\/?span' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected5));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string5));

		$incorrect_string6 = "<ol><li>this string</li></ol><dl><dt>is slightly</dt><dd>incorrect</dd></dl>";

		$expected6 = "/tag '\/?d[ldt]' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(6), $this->matchesRegularExpression($expected6));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string6));

		$incorrect_string7 = "<h1><big>this string</big></h1><h2>is</h2> <h4>slightly</h4><h3>incorrect</h3>";

		$expected7 = "/tag '\/?big' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected7));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string7));

		$incorrect_string8 = "<p>this <del>string</del> is <pre>slightly</pre> <tt>incorrect</tt></p>";

		$expected8 = "/tag '\/?del' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected8));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string8));

		$incorrect_string9 = "this<wbr> string<br> is<hr/> slightly<hr> incorrect";

		$expected9 = "/tag 'wbr' wordt niet herkend of toegelaten voor deze invoer \[$fieldname\]/";
		$this->add_alerter_to_input_checker($checker, $this->once(), $this->matchesRegularExpression($expected9));
		$this->assertFalse($checker->check_tags_advanced($incorrect_string9));
	}

	public function test_check_answer_correct() {
		$checker = new InputChecker();

		$alerter = $this->getMock('Alerter');
		$alerter->expects($this->never())
				->method('add_alert');

		$values = array("answer_1" => "plain text string",
						"answer_2" => "<b><i>this string <u>is</u> <sup>completely</sup> <sub>correct</sub></i></b>");

		$this->assertTrue($checker->checkAnswer(true, $values, 1));

		$this->assertTrue($checker->checkAnswer(true, $values, 2));

		$this->assertFalse($checker->checkAnswer(false, $values, 1));

		$this->assertFalse($checker->checkAnswer(false, $values, 2));
	}

	public function test_check_answer_incorrect() {
		$checker = new InputChecker();

		$values = array("answer_1" => "",
						"answer_2" => "<b><i>this string <strong>is</strong> <sup>slightly</sup> <sub>incorrect</sub></i></b>");


		$expected = "Geen invoer voor veld [antwoord #1]";
		$this->add_alerter_to_input_checker($checker, $this->once(), $expected);
		$this->assertFalse($checker->checkAnswer(true, $values, 1));

		$expected = "/tag '\/?strong' wordt niet herkend of toegelaten voor deze invoer \[antwoord #2\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected));
		$this->assertFalse($checker->checkAnswer(true, $values, 2));

		$expected = "Geen invoer voor veld [antwoord #1]";
		$this->add_alerter_to_input_checker($checker, $this->once(), $expected);
		$this->assertFalse($checker->checkAnswer(false, $values, 1));

		$expected = "/tag '\/?strong' wordt niet herkend of toegelaten voor deze invoer \[antwoord #2\]/";
		$this->add_alerter_to_input_checker($checker, $this->exactly(2), $this->matchesRegularExpression($expected));
		$this->assertFalse($checker->checkAnswer(false, $values, 2));
	}

	public function test_check_extension() {

		require_once('../sys/config.inc');
		global $CONFIG;

		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$page = $this->getMock("Page");
		$key = 'extensions';
		$file_key  = 'testfile';

		$CONFIG[$key] = array('txt');

		$file = array("name" => "testfile.txt");
		$files = array($file_key => $file);

		$alerter->expects($this->never())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$this->assertTrue($checker->checkExtension($key, $file_key, $files));
	}

	public function test_check_extension_with_incorrect_extension() {

		require_once('../sys/config.inc');
		global $CONFIG;

		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$page = $this->getMock("Page");
		$key = 'extensions';
		$file_key  = 'testfile';

		$CONFIG[$key] = array('txt');

		$file = array("name" => "testfile.doc");
		$files = array($file_key => $file);

		$expected = "Bestand heeft ongeldig type (enkel: txt)";

		$alerter->expects($this->once())
				->method('add_alert')
				->with($this->equalTo($expected));

		$checker->set_alerter($alerter);

		$result = $checker->checkExtension($key, $file_key, $files);
		$this->assertFalse($result);
	}

	public function test_check_body()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$page = $this->getMock("Page");

		$alerter->expects($this->never())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$values = array("body" => "this string has no tags");

		$result = $checker->checkBody($values);
		$this->assertTrue($result);

		$values = array("body" => "<b>this string has</b> correct tags");

		$result = $checker->checkBody($values);
		$this->assertTrue($result);
	}

	public function test_check_body_incorrect_tags()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$page = $this->getMock("Page");

		$values = array("body" => "this <big>string</big> has tags that are not allowed");

		$alerter->expects($this->exactly(2))
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkBody($values);
		$this->assertFalse($result);
	}

	public function test_check_body_empty_string()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("body" => "    ");

		$alerter->expects($this->once())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkBody($values);
		$this->assertFalse($result);
	}

	public function test_check_link()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$alerter->expects($this->never())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$values = array("body" => "this string has no tags",
						"link" => "http://www.nowhereinparticular.com");


		$result = $checker->checkLink(true, $values);
		$this->assertTrue($result);

		$values = array("body" => "<b>this string has</b> correct tags",
						"link" => "http://www.nowhereinparticular.com");

		$result = $checker->checkLink(true, $values);
		$this->assertTrue($result);
	}

	public function test_check_link_incorrect_tags()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("body" => "this <big>string</big> has tags that are not allowed",
						"link" => "http://www.nowhereinparticular.com");

		$alerter->expects($this->exactly(2))
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkLink(true, $values);
		$this->assertFalse($result);
	}

	public function test_check_link_empty_string()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("body" => "    ",
						"link" => "http://www.nowhereinparticular.com");

		$alerter->expects($this->once())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkLink(true, $values);
		$this->assertFalse($result);
	}

	public function test_check_link_ignore()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$alerter->expects($this->never())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$values = array("body" => "this string has no tags",
						"link" => "http://www.nowhereinparticular.com");


		$result = $checker->checkLink(false, $values);
		$this->assertFalse($result);

		$values = array("body" => "<b>this string has</b> correct tags",
						"link" => "http://www.nowhereinparticular.com");

		$result = $checker->checkLink(false, $values);
		$this->assertFalse($result);
	}

	public function test_check_link_incorrect_tags_ignore()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("body" => "this <big>string</big> has tags that are not allowed",
						"link" => "http://www.nowhereinparticular.com");

		$alerter->expects($this->exactly(2))
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkLink(false, $values);
		$this->assertFalse($result);
	}

	public function test_check_link_empty_string_ignore()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("body" => "    ",
						"link" => "http://www.nowhereinparticular.com");

		$alerter->expects($this->once())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkLink(false, $values);
		$this->assertFalse($result);
	}

	public function test_check_chars() {
		
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$field_name = "field";

		$alerter->expects($this->never())
				->method('add_alert');

		$correct_string = "onlyAlphaNumericalCharacters";

		$this->assertTrue($checker->check_chars($correct_string, $field_name));
		
		$alerter = $this->getMock("Alerter", array('add_alert'));
		$checker->set_alerter($alerter);

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Er zijn voor deze invoer enkel alfanumerieke tekens toegestaan [$field_name]");

		$incorrect_string = "alpha-numerical characters with one other character thrown in";

		$this->assertFalse($checker->check_chars($incorrect_string, $field_name));
		
	}

	public function test_check_int() {
		
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$field_name = "field";

		$alerter->expects($this->never())
				->method('add_alert');

		$correct_string = "01234567";

		$this->assertTrue($checker->check_int($correct_string, $field_name));
		
		$alerter = $this->getMock("Alerter", array('add_alert'));
		$checker->set_alerter($alerter);

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Er zijn voor deze invoer enkel cijfers toegestaan [$field_name]");

		$incorrect_string = "0.123456";

		$this->assertFalse($checker->check_int($incorrect_string, $field_name));
		
	}

	public function test_check_email() {
		
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$field_name = "field";

		$alerter->expects($this->never())
				->method('add_alert');

		$correct_string = "this.is_AN.email@address.co.uk";

		$this->assertTrue($checker->check_email($correct_string, $field_name));
		
		$alerter = $this->getMock("Alerter", array('add_alert'));
		$checker->set_alerter($alerter);

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldig email-adres [$field_name]");

		$incorrect_string = "totally not an email address";

		$this->assertFalse($checker->check_email($incorrect_string, $field_name));
		
	}

	public function test_check_new()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$alerter->expects($this->never())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$values = array("new_subset" => "this string has no tags");


		$result = $checker->checkNew(true, $values);
		$this->assertTrue($result);

		$values = array("new_subset" => "<b>this string has</b> correct tags");

		$result = $checker->checkNew(true, $values);
		$this->assertTrue($result);
	}

	public function test_check_new_incorrect_tags()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("new_subset" => "this <big>string</big> has tags that are not allowed");

		$alerter->expects($this->exactly(2))
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkNew(true, $values);
		$this->assertFalse($result);
	}

	public function test_check_new_empty_string()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("new_subset" => "    ");

		$alerter->expects($this->once())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkNew(true, $values);
		$this->assertFalse($result);
	}

	public function test_check_new_ignore()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$alerter->expects($this->never())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$values = array("new_subset" => "this string has no tags");


		$result = $checker->checkNew(false, $values);
		$this->assertFalse($result);

		$values = array("new_subset" => "<b>this string has</b> correct tags");

		$result = $checker->checkNew(false, $values);
		$this->assertFalse($result);
	}

	public function test_check_new_incorrect_tags_ignore()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("new_subset" => "this <big>string</big> has tags that are not allowed");

		$alerter->expects($this->exactly(2))
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkNew(false, $values);
		$this->assertFalse($result);
	}

	public function test_check_new_empty_string_ignore()
	{
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$values = array("new_subset" => "    ");

		$alerter->expects($this->once())
				->method('add_alert');

		$checker->set_alerter($alerter);

		$result = $checker->checkNew(false, $values);
		$this->assertFalse($result);
	}

	public function test_check_tag() {

		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$field_name = "field";
		$tag = "i";

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Tag '$tag' is niet toegestaan voor dit veld [$field_name]");


		$text = "<b>there is some</b> text in <i>italics at the end</i>";
		$this->assertFalse($checker->check_tag($tag, $text, $field_name));


		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$text = "<b>there is some</b> text in <em>italics at the end</em>";
		$tag = "i";

		$alerter->expects($this->never())
				->method('add_alert');

		$this->assertTrue($checker->check_tag($tag, $text, $field_name));
	}

	public function test_check_mail_input() {

		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$alerter->expects($this->never())
				->method('add_alert');

		$im = new InternalMail();

		$im->subject = "this is a valid subject";
		$im->body = "this is a valid body";

		$this->assertTrue($checker->check_mail_input($im));
	}

	public function test_check_mail_input_empty() {

		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$field_name = 'onderwerp';

		$checker->set_alerter($alerter);

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Geen invoer voor veld [$field_name]");

		$im = new InternalMail();

		$im->subject = "  ";
		$im->body = "	";

		$this->assertFalse($checker->check_mail_input($im));
	}

	public function test_check_date_valid_correct() {
		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$alerter->expects($this->never())
				->method('add_alert');

		$this->assertTrue($checker->check_date_valid(29, 6, 1982));
	}

	public function test_check_date_valid_too_early() {

		$checker = new InputChecker();
		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$field_name = 'field';

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(29, 6, 1897, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_january() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(32, 1, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_february() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(30, 2, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_march() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(32, 3, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_april() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(31, 4, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_may() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(32, 5, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_june() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(31, 6, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_july() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(32, 7, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_august() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(32, 8, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_september() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(31, 9, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_october() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(32, 10, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_november() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(31, 11, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_day_of_month_december() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(32, 12, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_zero_day() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(0, 12, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_zero_month() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(29, 0, 1982, $field_name));
	}

	public function test_check_date_valid_invalid_thirteenth_month() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);


		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$this->assertFalse($checker->check_date_valid(29, 13, 1982, $field_name));
	}

	public function test_check_date_future_by_one_day() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$alerter->expects($this->never())
				->method('add_alert');

		$minute_length = 60;
		$hour_length = 60 * $minute_length;
		$day_length = 24 * $hour_length;

		$time_stamp = time() + $day_length;
		
		$year = date('Y', $time_stamp);
		$month = date('n', $time_stamp);
		$day = date('j', $time_stamp);

		$this->assertTrue($checker->check_date_future($day, $month, $year, $field_name));
	}

	public function test_check_date_future_with_invalid_date() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Ongeldige datum voor invoer [$field_name]");

		$minute_length = 60;
		$hour_length = 60 * $minute_length;
		$day_length = 24 * $hour_length;

		$time_stamp = time() + $day_length;
		
		$year = date('Y', $time_stamp);
		$month = 13;
		$day = date('j', $time_stamp);

		$this->assertFalse($checker->check_date_future($day, $month, $year, $field_name));
	}

	public function test_check_date_future_by_one_month() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$alerter->expects($this->never())
				->method('add_alert');

		$minute_length = 60;
		$hour_length = 60 * $minute_length;
		$day_length = 24 * $hour_length;
		$month_length = 31 * $day_length;

		$time_stamp = time() + $month_length;
		
		$year = date('Y', $time_stamp);
		$month = date('n', $time_stamp);
		$day = date('j', $time_stamp);

		$this->assertTrue($checker->check_date_future($day, $month, $year, $field_name));
	}

	public function test_check_date_future_by_one_year() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$alerter->expects($this->never())
				->method('add_alert');

		$minute_length = 60;
		$hour_length = 60 * $minute_length;
		$day_length = 24 * $hour_length;
		$year_length = 366 * $day_length;

		$time_stamp = time() + $year_length;
		
		$year = date('Y', $time_stamp);
		$month = date('n', $time_stamp);
		$day = date('j', $time_stamp);

		$this->assertTrue($checker->check_date_future($day, $month, $year, $field_name));
	}

	public function test_check_date_incorrect_today() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$time_stamp = time();
		
		$year = date('Y', $time_stamp);
		$month = date('n', $time_stamp);
		$day = date('j', $time_stamp);

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Datum '$day/$month/$year' valt in het verleden [$field_name]");

		$this->assertFalse($checker->check_date_future($day, $month, $year, $field_name));
	}

	public function test_check_date_incorrect_one_month_ago() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$minute_length = 60;
		$hour_length = 60 * $minute_length;
		$day_length = 24 * $hour_length;
		$month_length = 31 * $day_length;

		$time_stamp = time() - $month_length;
		
		$year = date('Y', $time_stamp);
		$month = date('n', $time_stamp);
		$day = date('j', $time_stamp);

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Datum '$day/$month/$year' valt in het verleden [$field_name]");

		$this->assertFalse($checker->check_date_future($day, $month, $year, $field_name));
	}

	public function test_check_time_correct() {
		
		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$alerter->expects($this->never())
				->method('add_alert');

		$this->assertTrue($checker->check_time(12, 00, $field_name));
	}

	public function test_check_time_hour_too_late() {
		
		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$hours = 24;
		$mins = 0;

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Tijdstip $hours:$mins is niet geldig als invoer [$field_name]");


		$this->assertFalse($checker->check_time($hours, $mins, $field_name));
	}

	public function test_check_time_hour_too_early() {
		
		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$hours = -1;
		$mins = 0;

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Tijdstip $hours:$mins is niet geldig als invoer [$field_name]");


		$this->assertFalse($checker->check_time($hours, $mins, $field_name));
	}

	public function test_check_time_minutes_too_late() {
		
		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$hours = 12;
		$mins = 61;

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Tijdstip $hours:$mins is niet geldig als invoer [$field_name]");


		$this->assertFalse($checker->check_time($hours, $mins, $field_name));
	}

	public function test_check_time_minutes_too_early() {
		
		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$hours = 12;
		$mins = -1;

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Tijdstip $hours:$mins is niet geldig als invoer [$field_name]");


		$this->assertFalse($checker->check_time($hours, $mins, $field_name));
	}

	public function test_check_date_incorrect_one_year_ago() {

		$checker = new InputChecker();
		$field_name = 'field';

		$alerter = $this->getMock("Alerter", array('add_alert'));

		$checker->set_alerter($alerter);

		$minute_length = 60;
		$hour_length = 60 * $minute_length;
		$day_length = 24 * $hour_length;
		$year_length = 366 * $day_length;

		$time_stamp = time() - $year_length;
		
		$year = date('Y', $time_stamp);
		$month = date('n', $time_stamp);
		$day = date('j', $time_stamp);
		$day += 1;

		$alerter->expects($this->once())
				->method('add_alert')
				->with("Datum '$day/$month/$year' valt in het verleden [$field_name]");

		$this->assertFalse($checker->check_date_future($day, $month, $year, $field_name));
	}

	private function add_alerter_to_input_checker($checker, $expects, $alert_value = null) {

		$alerter = $this->getMock('Alerter');

		if($alert_value !== null)
		{
			$alerter->expects($expects)
					->method('add_alert')
					->with($alert_value);
		} else {
			$alerter->expects($expects)
					->method('add_alert');
		}

		$checker->set_alerter($alerter);
	}
}
